﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace Nirvana
{
    public class AnimatorTimelineBehaviour : StateMachineBehaviour
    {
        [Serializable]
        public sealed class TimelineAction
        {
            /// <summary>
            /// 动画归一化时间，用于触发动画事件
            /// </summary>
            [Tooltip("The normalized time when the time line triggered.")]
            [SerializeField]
            private float normalizedTime;

            [Tooltip("The time line event action.")]
            [SerializeField]
            private AnimatorEvent action;

            private int curNormalizedTime;
            private bool isTrigger;

            public float NormalizedTime
            {
                get { return this.normalizedTime; }
                set { this.normalizedTime = value; }
            }

            public string EventName
            {
                get { return this.action.EventName(); }
            }

            public string EventParam
            {
                get { return this.action.EventParam(); }
            }

            public int CurNormalizedTime
            {
                get { return curNormalizedTime; }
                set { curNormalizedTime = value; }
            }

            public bool IsTrigger
            {
                get { return this.isTrigger; }
                set { this.isTrigger = value; }
            }

            public void Excute(Animator animator,AnimatorStateInfo info)
            {
                this.action.Excute(animator, info);
            }

        }

        [SerializeField]
        [Tooltip("The time line event and actions.")]
        private TimelineAction[] timelineEvents;
        public TimelineAction[] TimelineEvents { get { return this.timelineEvents; } }

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
             for(int i=0,imax = this.timelineEvents.Length; i < imax; i++)
            {
                timelineEvents[i].IsTrigger = false;
                timelineEvents[i].CurNormalizedTime = 0;
            }
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {

            for (int i = 0, imax = this.timelineEvents.Length; i < imax; i++)
            {
                int normalizedTime = Mathf.FloorToInt(stateInfo.normalizedTime);
                if (normalizedTime > timelineEvents[i].CurNormalizedTime)
                {
                    if (timelineEvents[i].IsTrigger)
                    {
                        timelineEvents[i].IsTrigger = false;
                    }
                    else if (timelineEvents[i].NormalizedTime >= 0 && timelineEvents[i].NormalizedTime <= 1f)
                    {
                        timelineEvents[i].Excute(animator, stateInfo);
                    }
                    timelineEvents[i].CurNormalizedTime = normalizedTime;
                }
                if (!timelineEvents[i].IsTrigger)
                {
                    float t = stateInfo.normalizedTime - normalizedTime;
                    if(t >= timelineEvents[i].NormalizedTime)
                    {
                        timelineEvents[i].Excute(animator, stateInfo);
                        timelineEvents[i].IsTrigger = true;
                    }
                }

            }
        }

    }
}

